package com.demo.controller;

import com.demo.until.GeneratePDF;
import com.demo.until.GetHtmlContent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(@RequestParam(required = false,defaultValue = "World") String name) throws Exception {

        Map<String, Object> paramMap = new HashMap<>();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        paramMap.put("date_time", dateTimeFormatter.format(LocalDateTime.now()));
        paramMap.put("date", dateTimeFormatter.format(LocalDateTime.now()).substring(0, 10));
        ClassLoader classLoader = GetHtmlContent.class.getClassLoader();
        URL resource = classLoader.getResource("html");
        String templateDirectory  =resource.toURI().getPath();

   /*     String templateContent = GetHtmlContent.getTemplateContent(templateDirectory, "demo.html", paramMap);
        System.out.println(templateContent);*/
        return "Hello" + name;
    }



    @GetMapping("/htmltopdf")
    public String htmltopdf() throws Exception {

        StringBuffer head = readTxtFileInResources("html/demo1");
        System.out.println(head);

        StringBuffer body = new StringBuffer();
        body.append("<body>");
        body.append("<table class=\"name11\">");
        body.append("   <tbody class=\"bodyname\">");
        body.append("   <tr>");
        body.append("       <th colspan=\"8\" class=\"name9\">个人简历</th>");
        body.append("   </tr>");

        body.append("   <tr>");
        body.append("       <th colspan=\"1\" class=\"name\">姓名</th>");
        body.append("       <td colspan=\"3\" class=\"nametd\">");
        body.append("           查询出来的名称：杨县");
        body.append("       </td>");
        body.append("       <th colspan=\"1\" class=\"name\">年龄</th>");
        body.append("       <td colspan=\"3\" class=\"nametd\">");
        body.append("           查询出来的年龄：30");
        body.append("       </td>");
        body.append("   </tr>");
        body.append("   </tbody>");
        body.append("</table>");
        body.append("</body>");
        body.append("</html>");
        head.append(body);
        System.out.println(head);
        GeneratePDF.html2Pdf(head.toString(), "E:\\pdfdemo\\demo.pdf");

        return "htmltopdf";
    }


    private StringBuffer readTxtFileInResources(String relPath) {
        StringBuffer sb = new StringBuffer();

        try (InputStream fileIs = this.getClass().getClassLoader().getResourceAsStream(relPath)) {
            final int maxSize = 1024;
            byte[] buff = new byte[maxSize];
            int read = 0;

            while ((read = fileIs.read(buff, 0, maxSize)) > 0) {
                sb.append(new String(buff, 0, read));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return sb;
    }
}
